package org.bookie.framework.services.impl;

import org.apache.tapestry.INamespace;
import org.apache.tapestry.pageload.ComponentClassProvider;
import org.apache.tapestry.pageload.ComponentClassProviderContext;
import org.apache.tapestry.services.ClassFinder;

/**
 * Custom implementation of the {@link ComponentClassProvider}
 * Uses our tricky component naming rules where component class names
 * are composed of the component name and "Component" suffix
 * 
 * @author zubairov
 *
 */
public class CustomComponentClassProvider implements ComponentClassProvider {

    /**
     * Property name where we can find a default component package
     */
	private static final String COMP_PACKAGE_PROP_NAME = "org.apache.tapestry.component-class-packages";
	
    private ClassFinder _classFinder;

    public String provideComponentClassName(
            ComponentClassProviderContext context)
    {
        INamespace namespace = context.getNamespace();
        String packages = namespace.getPropertyValue(COMP_PACKAGE_PROP_NAME);

        String componentClassName = context.getName().replace('/', '.') + "Component";

        Class clazz = _classFinder.findClass(packages, componentClassName);

        return clazz == null ? null : clazz.getName();
    }

    public void setClassFinder(ClassFinder classFinder)
    {
        _classFinder = classFinder;
    }

}
